package maumau;

import java.util.*;


public class MauMau_MyPlayer extends MauMau_Intf {
	private Random rand;
        public Vector<Episode> episode = new Vector<Episode>();
        HashMap<Integer, Aq> saqMap = new HashMap<Integer, Aq>();
        int counter = 0;
	
	MauMau_MyPlayer() {
        rand = new Random(0);
    };
    
    private int hashCode(int topCard, Vector<Integer> actions) {
        int hash = 5;
        hash = 29 * hash + topCard;
        hash = 29 * hash + (actions != null ? actions.hashCode() : 0);
        return hash;
    }
        
        
    // Overwrite methods you want to use
    public void newGame() {
        System.out.println(getName() + ": newGame called");
    };
    
	@Override
	public int step(MauMau_State state, Vector<Integer> actions, double reward,
			boolean terminal) {
            counter++;
		System.out.println(getName() + ": step-method called.   reward = " + reward + "    terminal = " + terminal);
		// Terminal state? No action has to be selected. Perform RL-updates (e.g. for Monte Carlo)
        if(terminal) {
            System.out.println(getName() + ": I am called because game has ended");
            System.out.println(counter);
            
            for(int i  = 0; i < episode.size(); i++) {
                System.out.println(episode.elementAt(i).getAction() + " - " + i);
            }
           
            return 0;
        }
        
        
        
        
        
        
        

        // Select random action
        int pos = rand.nextInt(actions.size());
        Integer i = actions.get(pos);
        
        
        episode.add(new Episode(hashCode(state.getOpenCard(), actions), i.intValue()));
       
        return i.intValue();
	}
     private class Episode {
            private int stateHash, action;
            
            public Episode(int stateHash, int action) {
                this.stateHash = stateHash;
                this.action = action;
            }
            
            public int getAction() {
                return action;
     }
        }  
     
     private class Aq {
         Double[] q;
         Integer[] actions;
         
         Aq(Double[] q, Integer[] actions) {
             this.q = q;
             this.actions = actions;
         }
         
     }
        
}
